package com.jshop.admin.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshop.admin.sys.dao.SysUserDao;
import com.jshop.admin.sys.entity.SysUserEntity;
import com.jshop.admin.sys.service.SysRoleService;
import com.jshop.admin.sys.service.SysUserRoleService;
import com.jshop.admin.sys.service.SysUserService;
import com.jshop.common.constant.AdminConstant;
import com.jshop.common.exception.ApiRRException;
import com.jshop.common.utils.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 */
@Service("sysUserServiceImpl")
//public class SysUserServiceImpl implements SysUserService {
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public List<String> queryAllPerms(Integer userId) {
        return sysUserDao.queryAllPerms(userId);
    }

    @Override
    public List<Integer> queryAllMenuId(Integer userId) {
        return sysUserDao.queryAllMenuId(userId);
    }

    @Override
    public SysUserEntity queryByUserName(String username) {
        return sysUserDao.queryByUserName(username);
    }

    @Override
    public SysUserEntity queryObject(Integer userId) {
        return sysUserDao.queryObject(userId);
    }

    @Override
    public SysUserEntity queryPassword(String password) {
        return sysUserDao.queryPassword(password);
    }

    @Override
    public List<SysUserEntity> queryList(Map<String, Object> map) {
        return sysUserDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysUserDao.queryTotal(map);
    }
    @Override
    public Integer querySysUserId(Integer merchantId){
     return    sysUserDao.querySysUserId(merchantId);
    }


    @Override
    @Transactional
    public void mySave(SysUserEntity user) {
        user.setCreateTime(new Date());
        //sha256加密
        user.setPassword(new Sha256Hash(AdminConstant.DEFAULT_PASS_WORD).toHex());
        sysUserDao.save(user);

        //检查角色是否越权
        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIds());
    }

    @Override
    @Transactional
    public void update(SysUserEntity user) {
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(new Sha256Hash(AdminConstant.DEFAULT_PASS_WORD).toHex());
        } else {
            user.setPassword(new Sha256Hash(user.getPassword()).toHex());
        }

        //检查角色是否越权
//        checkRole(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIds());
    }

    @Override
    @Transactional
    public void deleteBatch(Integer[] userId) {
        sysUserDao.deleteBatch(userId);
    }

    @Override
    public int updatePassword(Integer userId, String password, String newPassword) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassword);
        return sysUserDao.updatePassword(map);
    }


    @Override
    public SysUserEntity queryMobile(String mobile) {
        return sysUserDao.queryMobile(mobile);
    }

    @Override
    public int saveAll(List<SysUserEntity> ids) {
        return sysUserDao.saveAll(ids);
    }


    @Override
    public Boolean getUserName(String userName) {
       int i=  sysUserDao.getUserName(userName);
        if (i>0){
            return true;
        }
        return false;
    }

//    @Override
//    public Page<UserWindowDto> findPage(UserWindowDto userEntity, int pageNum) {
//        return null;
//    }

    /**
     * 检查角色是否越权
     */
    private void checkRole(SysUserEntity user) {
        //如果不是超级管理员，则需要判断用户的角色是否自己创建
        if (user.getCreateUserId() == AdminConstant.SUPER_ADMIN) {
            return;
        }

        //查询用户创建的角色列表
        List<Integer> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

//        //判断是否越权
//        if (!roleIdList.containsAll(user.getRoleIdList())) {
//            throw new ApiRRException("新增用户所选角色，不是本人创建");
//        }
    }


//    @Override
//    public Page<UserWindowDto> findPage(UserWindowDto userWindowDto, int pageNum) {
//        PageHelper.startPage(pageNum, Constant.pageSize);
//        sysUserDao.queryListByBean(userWindowDto);
//        return PageHelper.endPage();
//    }
}
